<?php

declare(strict_types=1);

namespace App\Controller\act;

use App\Amqp\Producer\TaskProgressProducer;
use App\Controller\AbstractController;
use App\Enum\EnumType;
use App\Service\act\LuckyWheelService;
use Hyperf\Amqp\Producer;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use function Hyperf\Support\env;

#[Controller(prefix: "act.luckywheel")]
class LuckyWheelController extends AbstractController
{

    /**
     * 是否触发幸运转盘
     * @return null
     */
    #[RequestMapping(path: 'istriggerluckywheel')]
    public function isTriggerLuckyWheel(){
        $uid = $this->request->getAttribute('uid');
        try {
            $res = LuckyWheelService::isTriggerLuckyWheel($uid);
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            $this->logger->alert('LuckyWheelController.isTriggerLuckyWheel.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 发放每日抽奖次数
     * @return null
     */
    #[RequestMapping(path: 'sendeverydaydrawnum')]
    public function sendEveryDayDrawNum(){
        $drawNum = $this->request->getQueryParams('draw_num');
        try {
            LuckyWheelService::sendEveryDayDrawNum($drawNum ? (int)$drawNum : 1);
            return $this->response->write('success');
        } catch (\Throwable $e) {
            $this->logger->alert('LuckyWheelController.sendEveryDayDrawNum.Exception：' . $e->getMessage());
            return $this->response->write($e->getCode());
        }
    }

    /**
     * 游戏收藏完成
     * @return null
     */
    #[RequestMapping(path: 'gamecollectcompleted')]
    public function gameCollectCompleted()
    {
        $uid = $this->request->getAttribute('uid');
        try {
            // 游戏收藏完成处理幸运转盘活动任务进度
            // 消息队列完成任务进度
            $producer = ApplicationContext::getContainer()->get(Producer::class);
            $producer->produce(new TaskProgressProducer([
                'uid' => $uid,
                'condition_type' => EnumType::TASK_CONDITION_TYPE_GAME_COLLECT,
                'progress' => 1,
                // 任务筛选参数
                'act_id' => env('ACT_LUCKY_WHEEL_ID', 0)
            ]));

            $this->ReturnJson->successFul();
        } catch (\Throwable $e) {
            $this->logger->alert('LuckyWheelController.gameCollectCompleted.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }

    /**
     * 获取中奖用户数据
     * @return null
     */
    #[RequestMapping(path: 'getwinuserdata')]
    public function getWinUserData()
    {
        try {
            $res = LuckyWheelService::winUserData();
            return $this->ReturnJson->successFul(200, $res);
        } catch (\Throwable $e) {
            $this->logger->alert('LuckyWheelController.getWinUserData.Exception：' . $e->getMessage());
            return $this->ReturnJson->failFul($e->getCode());
        }
    }
}
